package com.infomanage.service.study;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import com.infomanage.authority.AuthorityBean;
import com.infomanage.bean.FieldBean;
import com.infomanage.bean.PaginationBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.dao.StudyPhysicalDAO;
import com.infomanage.dao.StudyRescueDAO;
import com.infomanage.enums.RoleType;
import com.infomanage.po.StudyRescue;
import com.infomanage.po.User;
import com.infomanage.util.Consts;
import com.infomanage.util.StringUtil;
import com.infomanage.vo.StudyRescueItem;
import com.jiuqi.util.ReturnObject;
import com.jiuqi.util.StringUtils;

public class StudyRescueService {

	public static StudyRescueItem loadStudyRescue(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			String itemId= request.getParameter("itemId");
			if(StringUtils.isEmpty(itemId)) return null;
			StudyRescueDAO dao = new StudyRescueDAO();
			return dao.find(Integer.parseInt(itemId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static List<StudyRescueItem> loadStudyRescueList(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			StudyRescueDAO dao = new StudyRescueDAO();
			List<StudyRescueItem> list = dao.findAll();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static ReturnObject saveStudyRescue(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			StudyRescue study = new StudyRescue();
			study.setDescription(request.getParameter("description"));
			study.setDetail(request.getParameter("detail"));
			study.setParticipants(request.getParameter("participants"));
			study.setPlace(request.getParameter("place"));
			study.setTimestamp(request.getParameter("timestamp"));
			study.setCommanderId(Integer.parseInt(request.getParameter("commanderId")));
			study.setDeptId(Integer.parseInt(request.getParameter("deptId")));
			study.setExcuteTimestamp(request.getParameter("excuteTimestamp"));
			study.setProblem(request.getParameter("problem"));
			study.setSubjects(request.getParameter("subjects"));
			StudyRescueDAO dao = new StudyRescueDAO();
			dao.add(study);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}
	
	public static ReturnObject removeStudyRescue(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String itemId = request.getParameter("itemId");
			StudyRescueDAO dao = new StudyRescueDAO();
			dao.remove(StudyRescue.class, Integer.parseInt(itemId));
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}
	
	public static ReturnObject removeBatchStudyRescue(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String batchIds = request.getParameter("batchIds");
			StudyRescueDAO dao = new StudyRescueDAO();
			if(batchIds != null){
				String[] ids = batchIds.split(";");
				for(String id : ids){
					dao.remove(StudyRescue.class, Integer.parseInt(id));
				}
			}
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}
	
	
	public static ReturnObject modifyStudyRescue(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			int itemId = Integer.parseInt(request.getParameter("itemId"));
			StudyRescueDAO dao = new StudyRescueDAO();
			StudyRescue study = dao.find(StudyRescue.class, itemId);
			study.setDescription(request.getParameter("description"));
			study.setDetail(request.getParameter("detail"));
			study.setParticipants(request.getParameter("participants"));
			study.setPlace(request.getParameter("place"));
			study.setTimestamp(request.getParameter("timestamp"));
			study.setCommanderId(Integer.parseInt(request.getParameter("commanderId")));
			study.setDeptId(Integer.parseInt(request.getParameter("deptId")));
			study.setExcuteTimestamp(request.getParameter("excuteTimestamp"));
			study.setProblem(request.getParameter("problem"));
			study.setSubjects(request.getParameter("subjects"));
			dao.modify(study);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
		}
		return ret;
	}
	
	public static List<StudyRescueItem> search(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			StudyRescueDAO dao = new StudyRescueDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			//设置分页
			String currPageNum = request.getParameter("currPageNum");
			PaginationBean page = new PaginationBean();
			if(currPageNum == null){
				page.setCurrentPageNum(1);
			}else{
				page.setCurrentPageNum(Integer.parseInt(currPageNum));
				
			}
			searchBean.setPage(page);
			List<StudyRescueItem> list = dao.findByFields(searchBean);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static int getPageCount(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			StudyRescueDAO dao = new StudyRescueDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			return (dao.count(searchBean) + PaginationBean.pageSize - 1)/PaginationBean.pageSize;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}
	
	private static SearchBean buildSearchCondition(HttpServletRequest request, HttpServletResponse response, JspWriter out) throws Exception{
		request.setCharacterEncoding("UTF-8");
		String excuteTimestamp = request.getParameter("excuteTimestamp");
		String place = request.getParameter("place");
		String commanderId = request.getParameter("commanderId");
		SearchBean searchBean = new SearchBean();
		if(!StringUtil.isEmpty(excuteTimestamp)){
			FieldBean field = new FieldBean("excuteTimestamp", excuteTimestamp);
			searchBean.add(field);
		}
		if(!StringUtil.isEmpty(place)){
			FieldBean field = new FieldBean("place", place);
			field.setLike(true);
			searchBean.add(field);
		}
		if(!StringUtil.isEmpty(commanderId)){
			FieldBean field = new FieldBean("commanderId", commanderId);
			searchBean.add(field);
		}
		//设置权限
		User user = (User) request.getSession().getAttribute(Consts.LOGINED_USER);
		if(user != null){
			AuthorityBean authority = new AuthorityBean();
			authority.setRole(RoleType.valueOf(user.getRoleId()));
			authority.setDeptId(user.getAssociatedDept());
			searchBean.setAuthority(authority);
		}
	return searchBean;
}
}
